package dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dao.BaseDao;
import dao.EBookDao;
import entity.EBook;
import util.PageUtil;

public class EBookDaoImpl extends BaseDao implements EBookDao {

	static{
		int a=1;
		
	}
	
	@Override
	public int getCount() {
		
		
		
		Map<Object, Object> maps=new HashMap<Object, Object>();
		maps.compute(null,null);
		  conn=getCon();
		  String sql="select count(1) from ebookentry";
		  try {
			ps=conn.prepareStatement(sql);
			  rs=ps.executeQuery();
			  if(rs.next()){
				  return rs.getInt(1);
			  }
			  
			  
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			super.closeAll(rs, ps, conn);
		}
		return 0;
	}

	@Override
	public List<EBook> ebookByPage(PageUtil page) {
		List<EBook> books=new ArrayList<EBook>();
		   conn=getCon();
		   String sql="select * from ebookentry limit ?,?";
		
		    try {
				ps=conn.prepareStatement(sql);
				ps.setInt(1, (page.getCurrentPage()-1)*page.getPageSize());
		        ps.setInt(2, page.getPageSize());
		        rs=ps.executeQuery();
		        while(rs.next()){
		        	
		        	 EBook book=new EBook(rs.getInt(1), rs.getString(3), rs.getInt(2),rs.getDate(6), rs.getString(5),rs.getString(4));
		             books.add(book);
		        }
		    } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				super.closeAll(rs, ps, conn);
			}
		   
		
		return books;
	}

}
